package com.ihealthway.all.groupbuy.modules.admin;

import com.ihealthway.all.slog.annotation.SLog;
import com.ihealthway.all.commons.Constants;
import com.ihealthway.all.groupbuy.services.SysDictService;
import com.ihealthway.all.utils.Ajax;
import com.ihealthway.all.utils.AjaxReturn;
import com.ihealthway.all.groupbuy.vo.SystemDict;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.List;
import java.util.Map;

/**
 * System dict module class
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
@IocBean
@At("/admin/sysdict")
public class SysDictModule {

    private static final Log logger = Logs.get();

    @Inject
    private SysDictService sysDictService;

    @At("/index")
    @Ok("beetl:admin/sysdict.jsp")
    public void defPage() {

    }

    @POST
    @At
    public List<SystemDict> findParentDict() {

        List<SystemDict> dicts = sysDictService.getFirstLevelDict();

        return dicts;
    }

    @POST
    @At
    public AjaxReturn checkDict(@Param("dictVal") String dictValue) {

        AjaxReturn ar = Ajax.ok();
        boolean flag = sysDictService.checkDitVal(dictValue);
        ar.setOk(flag);
        if (flag) {
            ar.setMsg("已经存在相同值域的字典,请重新填写.");
            ar.setErrCode("403");
        }

        return ar;
    }

    @POST
    @At
    @SLog(module = Constants.MODULE_SYSTEM, type = Constants.OPERATE_TYPE_ADD, content = "新增【${args[0].dictName}】字典信息")
    public AjaxReturn addNew(@Param("..")SystemDict sysDict) {

        AjaxReturn ar = Ajax.ok();
        boolean flag = sysDictService.add(sysDict);
        ar.setOk(flag);
        if (!flag) {
            ar.setMsg("新增字典信息失败,请联系管理员.");
            ar.setErrCode("500");
        }

        return ar;
    }

    @POST
    @At
    @SLog(module = Constants.MODULE_SYSTEM, type = Constants.OPERATE_TYPE_UPDATE, content = "更新【${args[0].dictName}】字典信息")
    public AjaxReturn update(@Param("..")SystemDict sysDict) {

        AjaxReturn ar = Ajax.ok();
        boolean flag = sysDictService.update(sysDict);
        ar.setOk(flag);
        if (!flag) {
            ar.setMsg("更新字典信息失败,请联系管理员.");
            ar.setErrCode("500");
        }

        return ar;
    }

    @POST
    @At
    public Object dataList(int id, int page, int rows) {

        Map<String, Object> map = sysDictService.getGridData(id, page, rows);

        return map.get("rows");
    }
}
